package com.circle.model.util;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import java.util.concurrent.TimeUnit;

public class OkHttpFactory {
    private static final int CONNECT_TIMEOUT = 10;
    private static final int READ_TIMEOUT = 10;
    private static final int WRITE_TIMEOUT = 10;
    
    private static volatile OkHttpClient client;

    /**
     * 获取OkHttpClient实例（单例模式）
     */
    public static OkHttpClient getClient() {
        if (client == null) {
            synchronized (OkHttpFactory.class) {
                if (client == null) {
                    // 创建日志拦截器
                    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
                    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

                    // 构建 OkHttpClient
                    client = new OkHttpClient.Builder()
                            .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                            .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                            .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                            .addInterceptor(loggingInterceptor)
                            .build();
                }
            }
        }
        return client;
    }
}
